package leetCode.fourthWeek;

import java.util.LinkedList;
import java.util.List;

public class Code_82 {
  public ListNode deleteDuplicates(ListNode head) {
    if (head == null) {
      return head;
    }
    ListNode tmp = head;
    List<Integer> multi = new LinkedList<>();
    while (tmp.next != null) {
      if (tmp.val == tmp.next.val) {
        multi.add(tmp.val);
        tmp.next = tmp.next.next;
      } else {
        tmp = tmp.next;
      }
    }
    tmp = head;
    while (tmp.next != null) {
      if (multi.contains(tmp.next.val)) {
        tmp.next = tmp.next.next;
      } else {
        tmp = tmp.next;
      }
    }
    if (multi.contains(head.val)) {
      return head.next;
    }
    return head;
  }
}
